package com.ruoyi.web.controller.agriculture;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.agriculture.domain.po.AgCropVariety;
import com.ruoyi.system.agriculture.domain.po.AgCropVariety;
import com.ruoyi.system.agriculture.service.IAgCropVarietyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 农作物品种Controller
 */
@RestController
@RequestMapping("/agriculture/variety")
public class AgCropVarietyController extends BaseController {
    @Autowired
    private IAgCropVarietyService cropVarietyService;

    /**
     * 查询农作物品种列表
     */
    @PreAuthorize("@ss.hasPermi('agriculture:variety:list')")
    @GetMapping("/list")
    public TableDataInfo list(AgCropVariety cropVariety) {
        startPage();
        LambdaQueryWrapper<AgCropVariety> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(cropVariety.getVarietyName() != null, AgCropVariety::getVarietyName, cropVariety.getVarietyName())
                .eq(cropVariety.getCropType() != null, AgCropVariety::getCropType, cropVariety.getCropType())
                .eq(cropVariety.getStatus() != null, AgCropVariety::getStatus, cropVariety.getStatus());
        List<AgCropVariety> list = cropVarietyService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出农作物品种列表
     */
    @PreAuthorize("@ss.hasPermi('agriculture:variety:export')")
    @Log(title = "农作物品种", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AgCropVariety cropVariety) {
        LambdaQueryWrapper<AgCropVariety> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(cropVariety.getVarietyName() != null, AgCropVariety::getVarietyName, cropVariety.getVarietyName())
                .eq(cropVariety.getCropType() != null, AgCropVariety::getCropType, cropVariety.getCropType())
                .eq(cropVariety.getStatus() != null, AgCropVariety::getStatus, cropVariety.getStatus());
        List<AgCropVariety> list = cropVarietyService.list(queryWrapper);
        ExcelUtil<AgCropVariety> util = new ExcelUtil<>(AgCropVariety.class);
        util.exportExcel(list, "农作物品种数据");
    }

    /**
     * 获取农作物品种详细信息
     */
    @PreAuthorize("@ss.hasPermi('agriculture:variety:query')")
    @GetMapping(value = "/{varietyId}")
    public AjaxResult getInfo(@PathVariable("varietyId") Long varietyId) {
        return success(cropVarietyService.getById(varietyId));
    }

    /**
     * 新增农作物品种
     */
    @PreAuthorize("@ss.hasPermi('agriculture:variety:add')")
    @Log(title = "农作物品种", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AgCropVariety cropVariety) {
        return toAjax(cropVarietyService.save(cropVariety));
    }

    /**
     * 修改农作物品种
     */
    @PreAuthorize("@ss.hasPermi('agriculture:variety:edit')")
    @Log(title = "农作物品种", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AgCropVariety cropVariety) {
        return toAjax(cropVarietyService.updateById(cropVariety));
    }

    /**
     * 删除农作物品种
     */
    @PreAuthorize("@ss.hasPermi('agriculture:variety:remove')")
    @Log(title = "农作物品种", businessType = BusinessType.DELETE)
    @DeleteMapping("/{varietyIds}")
    public AjaxResult remove(@PathVariable Long[] varietyIds) {
        return toAjax(cropVarietyService.removeBatchByIds(List.of(varietyIds)));
    }
} 